package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.customer.domain.Customer;
import com.ktgj.customer.domain.vo.MallCartVO;
import com.ktgj.customer.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.domain.MallCart;
import com.ktgj.customer.service.IMallCartService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 购物车Controller
 * 
 * @author ruoyi
 * @date 2022-05-30
 */
@RestController
@RequestMapping("/cart")
public class MallCartController extends BaseController
{
    @Autowired
    private IMallCartService mallCartService;
    @Autowired
    private ILoginService loginService;

    /**
     * 查询购物车列表
     */
    @PreAuthorize(hasPermi = "customer:cart:list")
    @GetMapping("/list")
    public TableDataInfo list(MallCart mallCart)
    {
        startPage();
        List<MallCart> list = mallCartService.selectMallCartList(mallCart);
        return getDataTable(list);
    }

    /**
     * 导出购物车列表
     */
    @PreAuthorize(hasPermi = "customer:cart:export")
    @Log(title = "购物车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MallCart mallCart) throws IOException
    {
        List<MallCart> list = mallCartService.selectMallCartList(mallCart);
        ExcelUtil<MallCart> util = new ExcelUtil<MallCart>(MallCart.class);
        util.exportExcel(response, list, "购物车数据");
    }

    /**
     * 获取购物车详细信息
     */
    @PreAuthorize(hasPermi = "customer:cart:query")
    @GetMapping(value = "/{cartId}")
    public AjaxResult getInfo(@PathVariable("cartId") Long cartId)
    {
        return AjaxResult.success(mallCartService.selectMallCartById(cartId));
    }

    /**
     * 新增购物车
     */
    @PreAuthorize(hasPermi = "customer:cart:add")
    @Log(title = "购物车", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallCart mallCart)
    {
        return toAjax(mallCartService.insertMallCart(mallCart));
    }

    /**
     * 修改购物车
     */
    @PreAuthorize(hasPermi = "customer:cart:edit")
    @Log(title = "购物车", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallCart mallCart)
    {
        return toAjax(mallCartService.updateMallCart(mallCart));
    }

    /**
     * 删除购物车
     */
    @PreAuthorize(hasPermi = "customer:cart:remove")
    @Log(title = "购物车", businessType = BusinessType.DELETE)
	@DeleteMapping("/{cartIds}")
    public AjaxResult remove(@PathVariable Long[] cartIds)
    {
        return toAjax(mallCartService.deleteMallCartByIds(cartIds));
    }

    /**
     * 购物车
     * @param request
     * @return
     */
    @PostMapping("/mallCartList")
    public AjaxResult mallCartList(HttpServletRequest request) {
        Customer customer;
        try {
            customer = loginService.getLoginCustomer(request);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        if (customer != null) {
            List<MallCartVO> mallCartVOS = null;
            if (customer.getOpenId() != null) {
                mallCartVOS = mallCartService.mallCartList(customer.getCustomerId(), customer.getOpenId());
            } else {
                mallCartVOS = mallCartService.mallCartList(customer.getCustomerId(), null);
            }
            if (mallCartVOS != null && mallCartVOS.size() > 0)
                return AjaxResult.success(mallCartVOS);
            else
                return AjaxResult.success("暂无商品");
        } else {
            return AjaxResult.error("01002", "登录已过期");
        }
    }

    /**
     * 购物车商品数量
     * @param request
     * @return
     */
    @PostMapping("/mallCartNum")
    public AjaxResult mallCartNum(HttpServletRequest request) {
        Customer customer;
        try {
            customer = loginService.getLoginCustomer(request);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        if (customer != null) {
            Long num = null;
            if (customer.getOpenId() != null) {
                num = mallCartService.mallCartNum(customer.getCustomerId(), customer.getOpenId());
            } else {
                num = mallCartService.mallCartNum(customer.getCustomerId(), null);
            }
            return AjaxResult.success(num);
        } else {
            return AjaxResult.error("01002", "登录已过期");
        }
    }
}
